package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.mapper.UserInfoMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
@MapperScan("com.tanhua.common.mapper")
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 根据所给用户id查询用户信息
     *
     * @param userId 用户id
     * @return 用户信息对象
     */
    public UserInfo queryUserInfoByUserId(Long userId) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.eq("user_id", userId);
        return userInfoMapper.selectOne(userInfoQueryWrapper);
    }

    public List<UserInfo> queryUserInfoList(QueryWrapper<UserInfo> queryWrapper) {
        return userInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户id集合查询用户信息
     *
     * @param userIdList 用户ID集合
     * @return 用户信息集合
     */
    public List<UserInfo> queryUserInfoListByUserIdList(Collection<?> userIdList) {
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.in("user_id", userIdList);
        return userInfoMapper.selectList(userInfoQueryWrapper);
    }

    public boolean updateUserInfoByUserId(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId());
        return userInfoMapper.update(userInfo, queryWrapper) > 0;
    }
}
